const Message = require('../models/messageModel')

class MessageController {
    /**
     * @swagger
     * /message/getMsgList:
     *   get:
     *     summary: 获取消息列表
     *     description: 获取消息列表
     *     tags: [消息相关]
     *     security:
     *       - BearerAuth: [] # 需要 Bearer 认证
     *     parameters:
     *       - name: currentPage
     *         in: query
     *         schema:
     *           type: number
     *         required: true
     *         description: 分页，当前页
     *       - name: pageSize
     *         in: query
     *         schema:
     *           type: number
     *         required: true
     *         description: 分页，每页显示的数量
     *     responses:
     *       200:
     *         description: 成功响应
     *         content:
     *           application/json:
     *             schema:
     *               type: object
     *               properties:
     *                 code:
     *                   type: number,
     *                   example: 200
     *                 data:
     *                   type: object
     *                   example: {"total":1,"rows":[{"id":10,"type":2,"isRead":0,"content":"admin 取关了你","state":0,"createTime":"2025-12-01 14:19:15"}]}
     *                 msg:
     *                   type: string
     *                   example: "查询成功！"
     */
    // 获取消息分类
    static async getMsgList(ctx) {
        try {
            const { userId } = ctx.state.user
            const { currentPage, pageSize } = ctx.query

            if (!userId) {
                ctx.body = { code: 500, data: null, msg: '请登录！' }
                return
            }

            if (
                currentPage === '' || currentPage === null || currentPage === undefined ||
                pageSize === '' || pageSize === null || pageSize === undefined
            ) {
                ctx.body = { code: 500, data: null, msg: '缺少核心参数！' }
                return
            }

            const result = await Message.getMsgList(userId, currentPage, pageSize)

            if (result) {
                if (result.length === 0) {
                    ctx.body = { code: 200, data: [], msg: '查询成功，但无数据！' }
                    return
                }

                ctx.body = { code: 200, data: result, msg: '查询成功！' }
            } else {
                ctx.body = { code: 500, data: null, msg: '查询失败！' }
            }
        } catch (error) {
            ctx.status = 500
            ctx.body = { code: 500, msg: '服务器错误，请联系管理员！' }
        }
    }

    /**
     * @swagger
     * /message/setMsgRead:
     *   put:
     *     summary: 设置消息已读
     *     description: 设置消息已读
     *     tags: [消息相关]
     *     security:
     *       - BearerAuth: [] # 需要 Bearer 认证
     *     parameters:
     *       - name: id
     *         in: query
     *         schema:
     *           type: number
     *         required: true
     *         description: 消息id
     *     responses:
     *       200:
     *         description: 成功响应
     *         content:
     *           application/json:
     *             schema:
     *               type: object
     *               properties:
     *                 code:
     *                   type: number,
     *                   example: 200
     *                 data:
     *                   type: object
     *                   example: null
     *                 msg:
     *                   type: string
     *                   example: "设置成功！"
     */
    // 设置某条消息已读
    static async setMsgRead(ctx) {
        try {
            const { userId } = ctx.state.user
            const { id } = ctx.request.body

            if (!userId) {
                ctx.body = { code: 500, data: null, msg: '请登录！' }
                return
            }

            if (id === '' || id === null || id === undefined) {
                ctx.body = { code: 500, data: null, msg: '缺少核心参数！' }
                return
            }

            const result = await Message.setMsgRead(userId, id)

            if (result.affectedRows > 0) {
                ctx.body = { code: 200, data: null, msg: '设置成功！' }
            } else {
                ctx.body = { code: 500, data: null, msg: '设置失败！' }
            }
        } catch (error) {
            ctx.status = 500
            ctx.body = { code: 500, msg: '服务器错误，请联系管理员！' }
        }
    }

    /**
     * @swagger
     * /message/getNoReadMsgCount:
     *   get:
     *     summary: 获取用户未读消息数量
     *     description: 获取用户未读消息数量
     *     tags: [消息相关]
     *     security:
     *       - BearerAuth: [] # 需要 Bearer 认证
     *     parameters:
     *     responses:
     *       200:
     *         description: 成功响应
     *         content:
     *           application/json:
     *             schema:
     *               type: object
     *               properties:
     *                 code:
     *                   type: number,
     *                   example: 200
     *                 data:
     *                   type: object
     *                   example: 1
     *                 msg:
     *                   type: string
     *                   example: "查询成功！"
     */
    // 获取用户未读消息数量
    static async getNoReadMsgCount(ctx) {
        try {
            const { userId } = ctx.state.user

            if (!userId) {
                ctx.body = { code: 500, data: null, msg: '请登录！' }
                return
            }

            const result = await Message.getNoReadMsgCount(userId)

            if (result) {
                ctx.body = { code: 200, data: result, msg: '查询成功！' }
            } else {
                ctx.body = { code: 500, data: null, msg: '查询失败！' }
            }
        } catch (error) {
            ctx.status = 500
            ctx.body = { code: 500, msg: '服务器错误，请联系管理员！' }
        }
    }
}

module.exports = MessageController
